package com.cloud.shopping.druid.util;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author John.zhang
 * @version Id: TimeUtil, v 1.0 2021/2/8 9:48 ZhiYong Exp $
 */
public class TimeUtil {

    public static final String FORMAT_1 = "yyyy-MM-dd HH:mm:ss";
    public static final String FORMAT_2 = "yyyy-MM-dd";
    public static final String FORMAT_3 = "yyyy年MM月dd日 HH时mm分ss秒";
    public static final String FORMAT_4 = "yyyy年MM月dd日";
    public static final String UNIT_HOUR = "hour";
    public static final String UNIT_DAY = "day";

    /**
     * 当前时间增加N Unit
     * @param unit unit
     * @param num num
     * @return String
     */
    public static String beforeTime(String unit,Integer num){
        //1小时为单位
        long times = 3600000;
        if(unit.equalsIgnoreCase(UNIT_HOUR)){
            times=times*num;
        }else if(unit.equalsIgnoreCase(UNIT_DAY)){
            times=times*24*num;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FORMAT_1);
        return simpleDateFormat.format( new Date(System.currentTimeMillis()-times));
    }

}
